package com.example.demo.file;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.annotation.JsonAppend;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.util.Arrays;
import java.util.List;

/**
 * @desc:
 * @author: fuxiang
 * @date: 2018/7/2 22:25
 */
public class Test {
    private static final String fileName = "f:/a/heh3.text";
    private static final String pathname = "f:/test.json";
    private static Logger logger= LoggerFactory.getLogger(Test.class);

    public static void main(String[] args) throws IOException {
        xieJson();
        duJson();
        File file=new File("f://a");
        if(!file.exists()){
            file.mkdirs();
        }

        File file1=new File("f:/a/heh3.text");
        try {
            file1.createNewFile();
            logger.info("创建文件成功");
        } catch (IOException e) {
            logger.error("创建文件异常",e);
            e.printStackTrace();
        }
        File file2 = new File("f:/a");
        File[] files = file2.listFiles();
        for (File file3 : files) {
            System.out.print(file3.isFile() + "  ");
            System.out.println(file3.getName());
        }

       //逐行读取文件
        try {
            FileReader fileReader=new FileReader(fileName);//定义一个file对象，用来初始化FileReader
            BufferedReader bufferedReader = new BufferedReader(fileReader);//new一个BufferedReader对象，将文件内容读取到缓存
            String s="";
            while ((s = bufferedReader.readLine())!=null){//逐行读取文件内容，不读取换行符和末尾的空格
                System.out.println(s);
             }
            bufferedReader.close();
        } catch (IOException e) {
            logger.error("讀取文件失敗",e);
            e.printStackTrace();
        }
       //将数据写入文件
        try {
            String ff="f:/a/testWrite.text";
            File file3 = new File(ff);
            if(!file3.exists()){
                file3.createNewFile();
            }
            FileWriter fileWriter=new FileWriter(ff);
            fileWriter.write("应该可以写入进来吧\n我试试呢2");
            logger.info("开始读取文件");
            String s = readTxt(ff);
            System.out.println(s);
            fileWriter.flush();
            fileWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /**
     * 逐行读取文件
     * @param fileName
     * @return
     */
    public static String readTxt(String fileName){
        try {
            FileReader fileReader2 = new FileReader(fileName);
            BufferedReader bufferedReader2=new BufferedReader(fileReader2);
            String s="",co="";
            while ((s=bufferedReader2.readLine())!=null){
                System.out.println(s);
            }
            return  co;
        } catch (IOException e) {
            logger.error("读取方法出错");
            e.printStackTrace();
        }
        return null;
    }
    public static void xieJson() throws IOException {
        File file=new File(pathname);
        if(!file.exists()){
            file.createNewFile();
        }
        JSONObject jsonObject=new JSONObject();
        for(int i=0;i<100;i++){
            jsonObject.put("test"+i,"testValue"+i);
        }
        FileWriter fileWriter=new FileWriter(pathname);
        fileWriter.write(JSON.toJSONString(jsonObject));
        fileWriter.flush();
        fileWriter.close();
    }
    public static void duJson() throws IOException {
        List list = FileUtils.readLines(new File(pathname));
        list.stream().forEach(System.out::println);
    }
    public static void duJson2() throws IOException {
        List list = FileUtils.readLines(new File("f://a//bb.txt"),"UTF-8");
        list.stream().forEach(System.out::println);
    }
}
